package com.turkline.CatchMePolygon;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import android.content.SharedPreferences;
import android.graphics.drawable.Drawable.ConstantState;

import com.badlogic.androidgames.framework.FileIO;
import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.impl.GLGame;

public class Settings
{
	public static String PREF_NAME = "CatchMePreferences"; 
	public static String SOUND_NAME = "Sound";
	public static String UNLUCKED_LEVELS = "UnlockedLevels";
	public static String LEVEL_SCORES = "LevelScores";
	public static String FIRST_HELP = "firstHelp";
	
	public static int CURRENT_LEVEL = 1;
	public static boolean soundEnabled = false;
	public static boolean firstHelpShowed = false;
	public final static int[] unlockedLevels = new int[]{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	public final static double[] levelScores = new double[]{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	public final static String file = "settings.catchMe";
	
	public static boolean gamePaused = false;
	public static boolean quiting = false;
	public static boolean killing = false;
	
	public static boolean showHelpStartScreen = false;
	
	
	
	public static void readPreferences(GLGame game)
	{
		SharedPreferences sp = game.getSharedPreferences(Settings.PREF_NAME, 0);
		soundEnabled = sp.getBoolean(Settings.SOUND_NAME, true);
		firstHelpShowed = sp.getBoolean(FIRST_HELP, false);
		int i = 0;
		
		for(i = 0 ; i < unlockedLevels.length ; i++)
		{
			int levelUnlocked = i == 0 ? 1 : 0;
			unlockedLevels[i] = sp.getInt(Settings.UNLUCKED_LEVELS + i, levelUnlocked);
			//unlockedLevels[i] = 1;
		}
		for(i = 0 ; i < levelScores.length ; i++)
		{
			levelScores[i] = Double.parseDouble(sp.getString(Settings.LEVEL_SCORES + i, "0"));
		}
		
	}
	
	public static void writePrefences(GLGame game)
	{
		SharedPreferences sp = game.getSharedPreferences(Settings.PREF_NAME, 0);
		SharedPreferences.Editor editor = sp.edit();
		editor.putBoolean(Settings.SOUND_NAME, soundEnabled);
		editor.putBoolean(Settings.FIRST_HELP, firstHelpShowed);
		int i = 0;
		for(i = 0 ; i < unlockedLevels.length ; i++)
		{
			editor.putInt(Settings.UNLUCKED_LEVELS + i , unlockedLevels[i]);
		}
		for(i = 0 ; i < levelScores.length ; i++)
		{
			editor.putString(Settings.LEVEL_SCORES+i, Double.toString(levelScores[i]));
		}
		
		editor.commit();
	}
	public static void record(int level, double score) 
	{
		if(level < unlockedLevels.length - 1 && score > World.BRONZE_SCORE) 
		{
			unlockedLevels[level] = 1;
			System.out.println("unlocked level : " + level);
		}
		if(levelScores[level-1] < score) 
		{
			levelScores[level-1] = score;
		}
		
	}
	
}
